package Test;

import java.util.*;

//懒汉模式 , 用的时候再去创建实例
class SingLazy{
    private volatile static SingLazy singLazy = null;
    private SingLazy(){};
    public static SingLazy getInstance(){
        synchronized (SingLazy.class){
            if(singLazy == null){
                if(singLazy == null)
                 singLazy = new SingLazy();
            }
        }

        return singLazy;
    }
}
//饿汉模式
class SingLegon{
    //自己去实例化一个对象
    static SingLegon singLegon = new SingLegon();
    //为了防止外界去new
    private SingLegon(){};
    //让对象返回唯一的实例
    public static SingLegon sing(){
        return singLegon;
    }
}
public class Test11 {
    public static void main(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>((a,b)->b-a);
        priorityQueue.offer(1);
        priorityQueue.offer(2);
        priorityQueue.offer(3);
        priorityQueue.offer(4);
        System.out.println(priorityQueue.poll());
    }
    public static void main6(String[] args) {
        SingLazy s1 = SingLazy.getInstance();
        SingLazy s2 = SingLazy.getInstance();
        System.out.println(s1 == s2);
    }
    public static void main5(String[] args) {
        SingLegon s1 = SingLegon.sing();
        SingLegon s2 = SingLegon.sing();
        System.out.println(s1 == s2);
    }
    public static void main4(String[] args) throws InterruptedException {
        Object obj = new Object();
        Thread t1 = new Thread(()->{
            synchronized (obj){
                System.out.println("执行t1线程,然后进入线程等待");
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        Thread.sleep(1000);
        Thread t2 = new Thread(()->{
            synchronized (obj){
                System.out.println("开始唤醒线程");
                obj.notify();
            }
        });
        t2.start();
    }
    public static void main3(String[] args) {
        Thread t = new Thread(()->{
            while (true){
                System.out.println("我是t线程");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread r = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("我是r线程");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
        r.start();

    }
    public static void main2(String[] args) {
        HashMap<Integer,Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);
        map.put(4,4);
        map.put(5,5);
        for(Integer key : map.keySet()){
            System.out.println(key+":"+map.get(key));
        }
    }
    //测试Collections.sort
    public static void main1(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(2);
        list.add(7);
        list.add(3);
        list.add(4);
        Collections.sort(list);
        System.out.println(list);
    }
}
